package com.feifan.web.controller.oa;

import com.feifan.common.annotation.Log;
import com.feifan.common.core.controller.BaseController;
import com.feifan.common.core.domain.AjaxResult;
import com.feifan.common.core.page.TableDataInfo;
import com.feifan.common.enums.BusinessType;
import com.feifan.framework.util.ShiroUtils;
import com.feifan.oa.domain.Leave;
import com.feifan.oa.domain.LeaveCheck;
import com.feifan.oa.service.ILeaveCheckService;
import com.feifan.oa.service.ILeaveService;
import com.feifan.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 请假审批Controller
 * @author 非凡
 * @date 2020-07-04
 */
@Controller
@RequestMapping("/oa/leaveCheck")
public class LeaveCheckController extends BaseController {
    private String prefix = "oa/sponsorFlow/leaveCheck";

    @Autowired
    private ILeaveService leaveService;

    @Autowired
    private ILeaveCheckService leaveCheckService;

    @RequiresPermissions("oa:leaveCheck:view")
    @GetMapping()
    public String leaveCheck() {
        return prefix + "/leaveCheck";
    }

    /**
     * 查询请假审批列表
     */
    @RequiresPermissions("oa:leaveCheck:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LeaveCheck leaveCheck) {
        startPage();
        List<LeaveCheck> list = leaveCheckService.setectLeaveCheckByUId(ShiroUtils.getUserId());
        return getDataTable(list);
    }

    /**
     * 获取审批详情
     */
    @RequiresPermissions("oa:leaveCheck:detail")
    @Log(title = "请假", businessType = BusinessType.OTHER)
    @GetMapping("/leaveCheckDetail/{id}")
    public String leaveCheckDetail(@PathVariable("id") String id, ModelMap mmap) {
        String[] str = id.split(",");
        mmap.put("leave", leaveService.selectLeaveById(Long.parseLong(str[0])));
        mmap.put("status", str[1]);
        return prefix + "/detail";
    }

    /**
     * 审批
     * @param id 请假单id
     * @return 审批记录集合
     */
    @PostMapping("/flowCheck")
    @ResponseBody
    public AjaxResult leaveCheckDetail(Integer type, Long id, String reject) {
        LeaveCheck leaveCheck = new LeaveCheck();
        //设置审批的用户
        SysUser sysUser = new SysUser();
        sysUser.setUserId(ShiroUtils.getUserId());
        leaveCheck.setCheckSysUser(sysUser);
        //设置审批的请假单id
        Leave leave = new Leave();
        leave.setId(id);
        leaveCheck.setLeave(leave);

        /**
         * type 审批类型
         * 1:通过
         * 2:驳回
         */
        if (type == 1) {
            leaveCheck.setStatus(2);
        } else if (type == 2) {
            leaveCheck.setStatus(3);
            leaveCheck.setRejectReason(reject);
            Leave leave2 = new Leave();
            leave2.setId(id);
            leave2.setStatus(3);
            leaveService.updateLeave(leave2);
        }
        //审批
        leaveCheckService.updateLeaveCheck(leaveCheck);

        //修改下一个审批人是否显示
        LeaveCheck leaveCheck1 = leaveCheckService.selectLeaveCheckById(leaveCheck.getId());
        if (!StringUtils.isEmpty(leaveCheck1.getNextSysUser())) {
            //设置审批的用户
            SysUser nextSysUser = leaveCheck1.getNextSysUser();
            leaveCheck1.setCheckSysUser(nextSysUser);
            //设置审批的请假单id
            Leave leave1 = new Leave();
            leave1.setId(id);
            leaveCheck1.setLeave(leave);
            //设置是否显示
            leaveCheck1.setIsShow(0);
            //设置状态
            leaveCheck1.setStatus(1);
            leaveCheckService.updateLeaveCheck(leaveCheck1);
        }

        //如果审批记录全部都通过就修改请假单的状态
        int count = 0;
        List<LeaveCheck> leaveChecks = leaveCheckService.selectLeaveCheckByLId(id);
        for (LeaveCheck check : leaveChecks) {
            if (check.getStatus() == 2) {
                count++;
            }
        }
        if (leaveChecks.size() == count) {
            Leave leave3 = new Leave();
            leave3.setId(id);
            leave3.setStatus(2);
            leaveService.updateLeave(leave3);
        }


        return AjaxResult.success();
    }
}
